
package as3.component.controls
{
        import com.seaengine.core.event.GlobalEvent;
        import com.seaengine.core.event.GlobalEventDispatcher;
        import com.seaengine.util.graphicUtils;
        
        import flash.display.Sprite;
        import flash.text.TextField;
        import flash.text.TextFieldAutoSize;
        import flash.text.TextFormat;
        import flash.utils.setTimeout;
        
        import gs.TweenNano;
        import gs.easing.Linear;
	
        public class MarqueeText extends Sprite
        {
                private var m_rect:Sprite;
                private var m_txt:TextField;
                //左移右移tmpflag
                private var m_bleftflag:Boolean;
                 private var m_delay:Number;
                //
                private var tween:TweenNano
                public function MarqueeText(text:String="", width:Number=100, delay:Number=1000)
                {
                        super();
                        //
                        m_delay=delay;
                        m_txt = new TextField();
                        m_txt.x = m_txt.y = 0;
                        m_txt.selectable = false;
                        m_txt.wordWrap = false;
                        m_txt.multiline = false;
                        m_txt.autoSize = TextFieldAutoSize.LEFT;
                        m_txt.htmlText = text;
                        addChild(m_txt);
                        //
                        //m_rect = new Rectangle(0, 0, width, m_txt.height);
                        m_rect = new Sprite();
                        graphicUtils.drawRect(m_rect,0,0,width, m_txt.height,0x0000FF,0.2);
                        m_rect.width=width;
                        m_rect.height=m_txt.height;
                        addChild(m_rect);
                        m_txt.mask = m_rect;
                        checkNeedTimer();
                }
                //只要是有可能改变文本的宽度或者scrollrect宽度的方法都应该调用该方法以刷新走马灯
                private function checkNeedTimer():void
                {
                	GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.SYSTEM_HIDE,true));
                    m_bleftflag = true;
                    m_txt.x =  m_rect.width+ m_rect.x;
                    tween = new TweenNano(m_txt, m_delay ,{x:-m_txt.textWidth-10,ease:Linear,onComplete:endFun});
                    //setTimeout(endFun,m_delay*1000);
                    //Tweener.addTween(m_txt,{x:-m_txt.textWidth-10,time:m_delay,transition:"linear",onComplete:endFun});
                }
                
                private function endFun():void
                {
                	GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.SYSTEM_HIDE,false));
                	if(tween){
	                	tween.kill();
	                	tween=null;
                	}
//                	if(parent)
//                	{
//                		parent.removeChild(this);
//                	}
//                	ChatProxy.getInstance().cacheArr.shift();
//                	ChatProxy.getInstance().addSystemMsg();
                }
               
                public function set text(text:String):void
                {
                        m_txt.htmlText = text;
                        checkNeedTimer();
                }
                public function get text():String
                {
                        return m_txt.htmlText;
                }
             
                public function set defaultTextFormat(format:TextFormat):void
                {
                        m_txt.defaultTextFormat = format;
                        setTextFormat(format);
                }
                public function setTextFormat(format:TextFormat, beginIndex:int = -1, endIndex:int = -1):void
                {
                        m_txt.setTextFormat(format, beginIndex, endIndex);
                        checkNeedTimer();
                }
            
        }
}